<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 银行模型
 * @author www.dingwei.cn
 */

class ConsumerBankModel extends MY_Model{
    public $tbUser = 'consumer';
    public $tbAdminBank = 'admin_bank';
    public $tbBank = 'consumer_bank_card';

    /**
     * 保存银行
     *
     * @param	array	$data	保存数据
     * @param	integer $id		用户id
     * @return	boolean
     */
    public function save($data, $id = 0){

        if( ! $data) return false;
        if($id){
            $this->db->where(array('id' => $id));
            if($this->update($this->tbBank, $data)){
                return true;
            }
        }else{
            if($id = $this->insert($this->tbBank, $data)){
                return $id;
            }
        }
    }

    /**
     * 后台获取用户银行
     *
     * @return array
     */
	public function adminGetList(){

        $consumerId = intval(get('consumer_id'));
        $account_name = trim(get('account_name'));
        $account_number = trim(get('account_number'));
        $mobile= trim(get('mobile'));
        $type = intval(get('bank_type'));//1-银行卡 2-三方

	    if(!$consumerId)return array();

	    if($account_name){
	        $this->db->like(array('b.account_name' => $account_name));
        }

        if($account_number){
            $this->db->like(array('b.account_number' => $account_number));
        }

        if($mobile){
            $this->db->where(array('b.mobile' => $mobile));
        }

        if($type){
            $this->db->where(array('b.type' => $type));
        }
	    $this->db->where(array('b.consumer_id' => $consumerId));

	    $this->db->select('b.id,b.account_name,b.account_number,b.mobile,b.open_bank,b.type')
                 ->join($this->tbUser.' u','u.id = b.consumer_id');

        return $this->pagination($this->tbBank.' b');
    }

    /**
     * 获取银行卡列表
     * @param integer $consumerId 消费者id
     * @param integer $type 	类型
     * @param integer $page
     * @return array|array[]|number[]
     * @author zyl
     */
    public function appGetBankList($consumerId, $type,$page = 1){
    	
    	$this->db
    	->select('id,icon,open_bank,type,account_name,account_number')
    	->where(array('consumer_id'=>$consumerId,'type'=>$type));
    	
    	$tA = $this->appPage($this->tbBank, $page,false,999);
    	
    	return $tA['list'];
    }
    
    /**
     * 获取银行卡
     * @param integer $id 卡id
     * @param number $consumerId
     * @return array|array
     * @author zyl
     */
    public function getInfo($id,$consumerId = 0){
    	if ( ! $id && ! $consumerId){
    		return array();
    	}
    	
    	if ($consumerId){
    		$this->db->where('consumer_id',$consumerId);
    	}
    	$this->db->where('id',$id);
    	
    	return $this->getOne($this->tbBank);
    }
    
    /**
     * 获取银行卡信息
     * @param integer $id 银行卡类型id
     * @return array
     * @author zyl
     */
    public function getAdminBank($id){
    	$this->db->where('id',$id);
    	$tA = $this->getOne($this->tbAdminBank);
    	if ($tA){
	    	$tA['bank_icon'] = getImage($tA['bank_icon'],'og');
    	}
    	return $tA;
    }
    
    /**
     * 隐藏号码
     * @param string $number 原始号码
     * @param number $start	 头部显示几位
     * @param number $end	尾部显示几位
     * @return unknown|string
     * @author zyl
     */
    public function HidesNumber($number,$start = 0,$end = 0){
    	if ( ! is_numeric($number)){
    		return $number;
    	}
    	
    	$length = strlen($number);
    	
    	if ($length < ($start + $end)){
    		return $number;
    	}
    	
    	$str = substr($number,0,$start);
    	$strend = substr($number,-$end);

        $str .=' ';
    	for ($n = 0; $n < $length-$start-$end;$n++){
    		$str .='*';
    		if ($n>2 && $n%3 == 0){
    		    $str .=' ';
            }
    	}
    	
    	$str.=$strend;
    	return $str;
    }
    
    /**
     * 获取账户类型
     * @param 	integer $status
     * @return	string
     */
    public function getBankType($status = null, $show = false){

        $config = array(1 => '银行卡', 2 => '支付宝');
        if( ! $status){
            return $config;
        }

        if($status && ! isset($config[$status])){
            return '';
        }

        if( ! $show){
            return $config[$status];
        }

        return'';
    }


}